package com.baizhiedu;

import com.baizhiedu.bean.ConnectionFactoryBean;
import com.baizhiedu.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 功能：
 * 作者：HZj
 * 日期：2025/1/5 18:52
 */

/*
本质：也是@Component注解的衍生注解
可以用用<context:component-scan进行扫描
*/
@Configuration
public class AppConfig {
    //@Bean注解在配置bean中进行使用，等同于XML配置文件中的<bean标签
    /**
     * 简单对象
     * @return
     * */
    @Bean("u")
//    @Scope("singleton")
    @Scope("prototype")
    public User user(){
        return new User();
    }

    /**
     * 创建复杂对象
     * Connection 不能直接通过new 创建的对象
     */
    @Bean
    public Connection conn(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/suns?useSSL=false","root","root");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    @Bean
    public Connection conn1(){
        Connection conn = null;
        try {
            ConnectionFactoryBean factoryBean = new ConnectionFactoryBean();
            conn = factoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}
